package com.lzy.imagepickerdemo.zn;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepickerdemo.GApp;
import com.lzy.imagepickerdemo.R;
import com.lzy.imagepickerdemo.imageloader.GlideImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddActivity extends AppCompatActivity {

    @Bind(R.id.ed_miaoshu)
    EditText edMiaoshu;
    @Bind(R.id.btn_add)
    Button btnAdd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        ButterKnife.bind(this);
        ImagePicker.getInstance().setShowCamera(true);
        ImagePicker.getInstance().isCheckShow=true;
        ImagePicker.getInstance().setImageLoader(new GlideImageLoader());
    }

    @OnClick(R.id.btn_add)
    public void onViewClicked() {
        String ed=edMiaoshu.getText().toString();
        if ("".equals(ed)){
            Toast.makeText(this,"请输入相册描述",Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent(this, ImageGridActivity.class);
        //ImagePicker.getInstance().setSelectedImages(images);
        startActivityForResult(intent, 100);


    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == 100) {
               List<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
              if (images!=null&&images.size()>0){
                  int photo_type=(int)(Math.random()*1000000);
                  String ed=edMiaoshu.getText().toString();
                  GApp.getInstance().mDBManager.addPhotoType(photo_type,ed);
                  for (ImageItem mImageItem:images){
                      ContentValues values = new ContentValues();
                      values.put("id",photo_type);
                      values.put("photo_type",photo_type);
                      values.put("img_id","");
                      values.put("img_url",mImageItem.path);
                      values.put("img_name",mImageItem.name);
                      GApp.getInstance().mDBManager.addPhotoList(values);
                  }
                    finish();
              }

            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
